import 'package:flutter/material.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/index.dart';
import 'package:yp/services/api.dart';
import 'package:yp/utils/util.dart';
import 'package:yp/widgets/index.dart';

/// 活动规则
class ActivityRule extends StatefulWidget {
  const ActivityRule({Key key}) : super(key: key);

  @override
  _ActivityRuleState createState() => _ActivityRuleState();
}

class _ActivityRuleState extends State<ActivityRule> {
  Map rule = {
    'label': '',
    'actName': '',
    'actTime': ['', ''],
    'platform': '',
    'giftTime': '',
    'actRule': []
  };
  bool visibles = false;

  getConsume() {
    var params = {
      "channel_num_id": 99,
      "sub_unit_num_id": 102002,
      // "ec_shop_num_id": SpUtil.getInt('storeShopNum'),
      "ec_shop_num_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      "award_types": null,
      'access_token': SpUtil.getString('token'),
    };
    Api.taskQuery(params).listen((event) {
      if (event['code'] == 0) {
        setState(() {
          if (event['member_surprise_task_infos'] != null &&
              event['member_surprise_task_infos'].length > 0) {
            event['member_surprise_task_infos'].forEach((element) {
              if (element['member_task_infos'] != null &&
                  element['member_task_infos'].length > 0 &&
                  element['award_type'] == 606) {
                var data = element['member_task_infos'][0];
                rule['label'] = data['award_type_name'];
                rule['actName'] = data['award_type_name'];
                rule['actTime'][0] = data['begin_dtme'];
                rule['actTime'][1] = data['end_dtme'];
                rule['platform'] =
                    getTaskScreen(data['member_task_rule']['platform'])
                        .toString();
                rule['giftTime'] = data['member_task_rule']['award_date_info'];
                rule['actRule'] = data['member_task_rule']['award_rule'];
              }
            });
          }
          visibles = true;
        });
      }
    });
  }

  @override
  void initState() {
    getConsume();

    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: YPNavigationBar('活动规则'),
        body: Padding(
          padding: const EdgeInsets.fromLTRB(12, 20, 12, 30),
          child: Visibility(
            visible: visibles,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  padding: EdgeInsets.symmetric(vertical: 2, horizontal: 4),
                  child: Text(
                    rule['label'],
                    style: TextStyle(
                        color: EsColors.White,
                        fontSize: EsSizes.XS,
                        fontWeight: EsFontWeights.Normal),
                  ),
                  decoration: BoxDecoration(
                    image: DecorationImage(
                        fit: BoxFit.cover,
                        image:
                            AssetImage('assets/images/acquiring_gift_bg.png')),
                  ),
                ),
                SizedBox(height: 10),
                Text(
                  rule['actName'],
                  style: TextStyle(
                      color: EsColors.Title,
                      fontSize: EsSizes.LG,
                      fontWeight: EsFontWeights.SemiBold),
                ),
                SizedBox(height: 10),
                _item('活动时间：', '${rule['actTime'][0]}至${rule['actTime'][1]}'),
                SizedBox(height: 10),
                _item('适用平台：', '${rule['platform']}'),
                SizedBox(height: 20),
                _item('发奖时间：', '${rule['giftTime']}'),
                SizedBox(height: 30),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Text(
                      '活动规则:',
                      style: TextStyle(
                          color: EsColors.TitleSecondary,
                          fontSize: EsSizes.SM,
                          fontWeight: EsFontWeights.Normal),
                    ),
                    Expanded(
                        child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: rule['actRule'].map<Widget>((e) {
                        return Text(
                          e.toString(),
                          style: TextStyle(
                              color: EsColors.Content,
                              fontSize: EsSizes.SM,
                              fontWeight: EsFontWeights.Normal),
                        );
                      }).toList(),
                    ))
                  ],
                ),
                Expanded(child: SizedBox()),
                SizedBox(
                  width: double.infinity,
                  child: Center(
                    child: Text(
                      '注：多活动共存时，叠加享受',
                      style: TextStyle(
                          color: EsColors.ContentSecondary,
                          fontSize: EsSizes.XXS,
                          fontWeight: EsFontWeights.Normal),
                    ),
                  ),
                )
              ],
            ),
          ),
        ));
  }

  Widget _item(String title, String context) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(
              color: EsColors.TitleSecondary,
              fontSize: EsSizes.SM,
              fontWeight: EsFontWeights.Normal),
        ),
        Expanded(
          child: Text(
            context,
            style: TextStyle(
                color: EsColors.Content,
                fontSize: EsSizes.SM,
                fontWeight: EsFontWeights.Normal),
          ),
        )
      ],
    );
  }
}
